﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private Maticsoft.BLL.tree _bll;

        private List<Maticsoft.Model.tree> _alllist;
        private Msg4JsonResult _msg;
        public HomeController()
        {
            _msg = new Msg4JsonResult();
            _bll = new Maticsoft.BLL.tree();
            _alllist = _bll.GetModelList("");//获取所有数据
        }
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Del(int id)
        {
            try
            {
                if(_bll.GetModelList("FatherClassID="+id).Count==0)
                _bll.Delete(id);
                else
                {
                    _msg.Success = false;
                    _msg.Msg = "错误信息：请先删除子项" ;
                }
            }
            catch (Exception ex)
            {
                _msg.Success = false;
                _msg.Msg = "错误信息："+ ex.Message;
                
            }

            return Json(_msg);
        }

        public ActionResult GetTreeJson()
        {
            return Json(GetTree(0),JsonRequestBehavior.AllowGet);
        }

        public List<TreeJson> GetTree(int pid)
        {
            var tree = _alllist.FirstOrDefault(s=>s.ClassID== pid);
            var list = _alllist.Where(s => s.FatherClassID == pid).ToList();//_bll.GetModelList(" FatherClassID=" + pid);

            var treelist = new List<TreeJson>();

            list.ForEach(s => {
                treelist.Add(new TreeJson()
                {
                    id = s.ClassID + "",
                    text = s.ClassName,
                    nodes = GetTree(s.ClassID)
                });
            });
            return treelist;

        }

        public ActionResult Op()
        {
            return View();
        }
        [HttpPost]
        public ActionResult OpSaveAsPDF()
        {
            return View();
        }
    }
}